#include					"Listener.h"
#include					"Encoder.h"
#include					"byte.h"
#include					<iostream>

Encoder::Encoder() : _enc(NULL), _channel(2)
{

}

Encoder::~Encoder()
{
	if (_enc != NULL)
		opus_encoder_destroy(_enc);
}

void						Encoder::init()
{
	int						error = 0;
	if (_enc != NULL)
		opus_encoder_destroy(_enc);
	_enc = opus_encoder_create(48000, _channel, OPUS_APPLICATION_VOIP, &error);
	if (error != OPUS_OK)
		std::cout << "error in opus_encoder_create" << std::endl;
}

void						Encoder::encode(ISoundPacket *packet, IEncodedSoundPacket *encpacket)
{
	int						size;
	byte					*output = new byte[PACKET_SIZE];
	
	size = opus_encode(_enc, packet->data(), packet->frame_size(), output, PACKET_SIZE);
	encpacket->setData(output);
	encpacket->setLen(size);
}

void						Encoder::encode(ISoundPacket *packet, IOrder *order)
{
	short					size;
	byte					*output = new byte[PACKET_SIZE];

	size = opus_encode(_enc, packet->data(), packet->frame_size(), output, PACKET_SIZE);
	order->pushData(&size, sizeof(short));
	order->pushData(output, size * sizeof(byte));
}
